package com.sixth.system.service.impl;/**
 * @author 633yh
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixth.core.entity.News;
import com.sixth.core.vo.R;
import com.sixth.system.mapper.NewsMapper;
import com.sixth.system.service.NewsService;
import com.sixth.system.vo.NewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author     ：Stu YH
 * @date       ：Created in 2024/8/21 9:36
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Override
    public R<Page<News>> listByPage(NewsVo newsVo) {
        Page<News> page = new Page<>(newsVo.getPageNum(), newsVo.getPageSize());
        QueryWrapper<News> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(newsVo.getNewsSource())){
            wrapper.eq("news_source",newsVo.getNewsSource());
        }
        if (newsVo.getDateRange()!=null&&newsVo.getDateRange().size()==2){
            wrapper.between("create_time",newsVo.getDateRange().get(0),newsVo.getDateRange().get(1));
        }
        Page<News> newsPage = newsMapper.selectPage(page, wrapper);
        return R.ok(newsPage);
    }

    @Override
    public boolean addUpdate(News news) {
        news.setCreateTime(new java.util.Date());
        return newsMapper.insertOrUpdate(news);
    }

    @Override
    public R delete(Long newsId) {
        newsMapper.deleteById(newsId);
        return R.ok();
    }

    @Override
    public R<List<News>> listAllNews() {
        return R.ok(newsMapper.selectList(null));
    }

    @Override
    public R<News> getNewsByNewsId(Long newsId) {
        QueryWrapper<News> wrapper = new QueryWrapper<>();
        wrapper.eq("news_id",newsId);
        return R.ok(newsMapper.selectOne(wrapper));
    }
}